package org.boulderfoodrescue.webapp.client.customtable;

import java.util.List;

import org.boulderfoodrescue.webapp.client.resources.Resources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class CustomCellTable<T> extends FlowPanel implements HasValue<T>{
	protected CellTable<T> table;
	protected ListDataProvider<T> dataProvider;
	private T value;
	private SimplePager pager;
	private CustomCellTableView<T> view;
	private SingleSelectionModel<T> selectionModel;

	public CustomCellTable(List<CustomColumn<T>> columns, Boolean enableSingleSelect, String emptyText){
		dataProvider = new ListDataProvider<T>();
		table = new CellTable<T>();

		//Add columns to the table
		for(CustomColumn<T> cc : columns){
			table.addColumn(cc.getColumn(), cc.getTitle());
		}

		//Add the dataprovider
		dataProvider.addDataDisplay(table);

		//Add a sort handler
		ListHandler<T> sortHandler = new ListHandler<T>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		//Enable single select if requested
		selectionModel = new SingleSelectionModel<T>();
		if(enableSingleSelect){
			table.setSelectionModel(selectionModel);
			selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
				@Override
				public void onSelectionChange(SelectionChangeEvent event) {
					value = selectionModel.getSelectedObject();
				}
			});
		}

		//Setup the pager
		SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
		pager.setDisplay(table);
		pager.setPageSize(10);

		//Setup the view
		view = new CustomCellTableView<T>(table, pager);
		this.add(view);

		//Set the empty text
		HTML emptyHTML = new HTML(emptyText);
		emptyHTML.addStyleName(Resources.INSTANCE.customTable().emptyTableText());
		table.setEmptyTableWidget(new HTML(emptyText));
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}

	@Override
	public T getValue() {
		return value;
	}

	@Override
	public void setValue(T value) {
		setValue(value, true);
	}

	@Override
	public void setValue(T value, boolean fireEvents) {
		if(this.value != null)
			selectionModel.setSelected(this.value, false);
		this.value = value;
		selectionModel.setSelected(value, true);
		if(fireEvents)
			ValueChangeEvent.fire(this, value);
	}
}
